package Events;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import structures.Pace;
import structures.Passenger;
import structures.Player;
import structures.Rations;
import structures.Spaceship;
import structures.Status;
import structures.Tradepost;
import Events.gui.SetupPassengersTemp;
import Events.gui.SetupPlayerTemp;
import Events.gui.SetupSpaceshipTemp;
import Events.gui.TradepostGUI;
import Professions.Profession;

public class StartGameEvent extends PathEvent{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2162399235607989235L;
	//private TradePost tradepost;
	private static String name = "StartGame";
	private static String description = "You've just started playing Space Trail. Good choice!";
	
	private Spaceship spaceship;
	private Tradepost tradepost = new Tradepost(this);
	
	protected JPanel[] phases = {new SetupPlayerTemp(this), new SetupPassengersTemp(this),
								new SetupSpaceshipTemp(this), new TradepostGUI(this.tradepost)};
	//took out new SetupFinishTemp(this) and replaced with TradepostStore 
	private int phaseIndex;
	
	public StartGameEvent(){
		super(name, description, 0);
		phaseIndex = 0;
		running = false;
		add(phases[phaseIndex]);
	}
	public Tradepost getTradepost(){
		return tradepost;
	}
	
	public void execute(Spaceship s){
		spaceship = s;
		tradepost.addCustomer(spaceship);
		((TradepostGUI)(phases[3])).updatePlayerStats();
		running = true;
		//create player screen
		//...GUI stuff
		//create party screen
		//...GUI stuff
		
		//pick actual spaceship screen
		//GO TO THE TRADE POST
		//JK, we're just gonna say you won now :P
	}
	
	public void execute(Spaceship s, JTextArea log) {
	    this.execute(s);
	}
	
	public boolean isRunning(){
		return running;
	}
	
	public void createPlayer(String name, Profession prof){
		//System.out.println("Creating Player");
		spaceship.getPlist().add(new Player(name, Status.HEALTHY, prof));
		//System.out.println(spaceship.getPlist().get(0).getName() + " : " 
		//		+ (((Player) spaceship.getPlist().get(0)).getProfession()) );
		nextPhase();
	}
	
	public void createParty(String[] party){
		//System.out.println("Creating Party");
		for(String name: party){
			spaceship.getPlist().add(new Passenger(name, Status.HEALTHY));
			//System.out.println(spaceship.getPlist().get(i++).getName());
		}
		nextPhase();
	}
	
	public void setPaceRations(Pace pace, Rations rations){
		//System.out.println("Setting pace and rations");
		spaceship.setPace(pace);
		spaceship.setRations(rations);
		//System.out.println("Pace: " + spaceship.getPace() + "Rations: " + spaceship.getRations());
		nextPhase();
	}
	
	public void nextPhase(){
		try{
			remove(phases[phaseIndex]);
			phaseIndex = phaseIndex + 1;//)%phases.length;
			add(phases[phaseIndex]);
			revalidate();
			repaint();
		} catch(Exception e){
			running = false;
			//System.out.println("Done");
		}
	}
	
	public void prevPhase(){
		remove(phases[phaseIndex]);
		phaseIndex = (phaseIndex - 1)%phases.length;
		add(phases[phaseIndex]);
		revalidate();
		repaint();
	}
	
}
